package com.movie.controller;

import com.movie.entity.Seat;
import com.movie.service.SeatService;
import com.movie.service.ShowtimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
public class SeatController {

    @Autowired
    private SeatService seatService;

    @Autowired
    private ShowtimeService showtimeService;

    /**
     * 根据场次ID获取座位信息
     * @param showtimeId 场次ID
     * @return 座位列表
     */
    @GetMapping("/showtimes/{showtimeId}/seats")
    public ResponseEntity<List<Seat>> getSeatsByShowtime(@PathVariable Long showtimeId) {
        try {
            List<Seat> seats = seatService.getSeatsByShowtimeId(showtimeId);
            return ResponseEntity.ok(seats);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 初始化场次座位
     * @param showtimeId 场次ID
     * @param rows 行数
     * @param columns 列数
     * @return 是否初始化成功
     */
    @PostMapping("/showtimes/{showtimeId}/seats/initialize")
    public ResponseEntity<String> initializeSeats(
            @PathVariable Long showtimeId,
            @RequestParam int rows,
            @RequestParam int columns) {
        try {
            boolean success = seatService.initializeSeats(showtimeId, rows, columns);
            if (success) {
                // 初始化座位成功后，更新场次的可用座位数量
                showtimeService.updateAvailableSeats(showtimeId);
                return ResponseEntity.ok("座位初始化成功");
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("座位初始化失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("座位初始化失败: " + e.getMessage());
        }
    }

    /**
     * 更新座位状态
     * @param id 座位ID
     * @param status 新状态
     * @return 是否更新成功
     */
    @PutMapping("/seats/{id}/status")
    public ResponseEntity<String> updateSeatStatus(
            @PathVariable Long id,
            @RequestParam String status) {
        try {
            // 先获取座位信息，以便获取场次ID
            Seat seat = seatService.getSeatById(id);
            if (seat == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("座位不存在");
            }

            boolean success = seatService.updateSeatStatus(id, status);
            if (success) {
                // 更新座位状态成功后，更新场次的可用座位数量
                showtimeService.updateAvailableSeats(seat.getShowtimeId());
                return ResponseEntity.ok("座位状态更新成功");
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("座位不存在或状态更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("座位状态更新失败: " + e.getMessage());
        }
    }
}
